I have a user input form where the user can choose to specify a number of input fields used for database input.

So what I did is to create a loop within the same form and then created the fields the number of times the user has specified.

Example: the user has specified the value of 3

Loop:
1
- Field1
- Field2
- Field3

2
- Field1
- Field2
- Field3

3
- Field1
- Field2
- Field3

So this mean we have values for 3 database records.

1. Field1, Field2, Field3
2. Field1, Field2, Field3
3. Field1, Field2, Field3

The problem is that the fields has the same names. It is possible to loop the entire form over and over again with a submit button attached to each form. But I also want to keep the user activity to a minimum and the user just have to submit all of the fields once. I have tried so send it all over to an array but there is problems splitting that up.

Any suggestions or advices is more than welcome.

Recommended Answers

All 4 Replies

This appears to be pretty simple to me, so maybe I'm not getting what you're trying to do.
But anyhew, I'd do smth like:

Dim intCount
intCount = Request("intCount") ' user entered how many fields shold be displayed

For i = 1 To intCount ' for the rows
   For j = 1 To 3 ' for the fields
      response.write "<input type=""textbox"" name=""field_" & i & "_" & j & """>"
   Next
Next

response.write "<input type=""hidden"" name=""intFieldCount"" value=""" & intCount & """">"
' write out the number of fields just produced

Then when the form is posted:

Dim intFieldCount
intFieldCount = Request.Form("intFieldCount") 'read back the number of field to loop through and create sql statement
For i = 1 To intFieldCount
   sqlStatement = "Inser Into....."
   For j = 1 To 3
      sqlStatement = sqlStatement & ...... Request.Form("field_" & i & "_" & j).....
   Next
   dbConnection.Execute sqlStatement
Next

Does that help?

Thanks

Almost there
Here is some more details about the use of the function. Each loop contain 7 fields.

The number can be anything from 1 to whatever upwards.
So if the user choose the number 2 then the fields below loops out 2 times

1. field1 field2 field3 field4 field5 field6 field7
2. field1 field2 field3 field4 field5 field6 field7

INPUT FORM

For i = 1 To number

 <input type="hidden" name="number" value="<%=number%>">

 <input type="text" name="field1">
 <input type="text" name="field2">
 <input type="text" name="field3">
 <input type="text" name="field4">
 <input type="text" name="field5">
 <input type="text" name="field6">
 <input type="text" name="field7">

Next

<input type='submit' value='SEND' name='B3'>

Before I send it to a database I need to send all fields to a validation function. But that is a later problem. I need to split each of the fieldsets into the specified number.

If the following values are sended away to the function below, then all of the values are mixed up.

--loop 1---------------
field1 = "A_1"
field2 = "A_2"
field3 = "A_3"
field4 = "A_4"
field5 = "A_5"
field6 = "A_6"
field7 = "A_7"
--loop 2---------------
field1 = "B_1"
field2 = "B_2"
field3 = "B_3"
field4 = "B_4"
field5 = "B_5"
field6 = "B_6"
field7 = "B_7"


FUNCTION

number = Request.Form("number")

field1 = Request.Form("field1")
field2 = Request.Form("field2")
field3 = Request.Form("field3")
field4 = Request.Form("field4")
field5 = Request.Form("field5")
field6 = Request.Form("field6")
field7 = Request.Form("field7")


For i = 1 To number

Response.Write(field1 &" "& field2&" "& field3&" "& field4&" "& field5&" "& field6&" "& field7) &"<br>"

Next

OUTPUT

A_1, B_1 A_2, B_2 A_3, B_3 A_4, B_4 A_5, B_5 A_6, B_6 A_7, B_7
A_1, B_1 A_2, B_2 A_3, B_3 A_4, B_4 A_5, B_5 A_6, B_6 A_7, B_7

Like I wrote in the first code snippets it would be wise to name ALL field uniquely.

If you look closer at then snippet with the two for-loops

For i = 1 To intCount ' for the rows
   For j = 1 To 3 ' for the fields
      response.write "<input type=""textbox"" name=""field_" & i & "_" & j & """>"
   Next
Next

that code will produce (assuming 2 rows with 3 fields each).

<input type="textbox" name="field_1_1">
<input type="textbox" name="field_1_2">
<input type="textbox" name="field_1_3">
<input type="textbox" name="field_2_1">
<input type="textbox" name="field_3_2">
<input type="textbox" name="field_4_3">

Then you have uniquely named fields, and can reference them accordingly.

Hi there!

Thanks again.
I solved it, but without the second loop (j).

Here it is!
From here it should not be any problem to create a validation routine for the fieldsets.

OUTPUT TESTCODE

For i = 0 To number -1

address = Request.Form("address_" & i )
zipcode = Request.Form("zipcode_" & i )
city = Request.Form("city_" & i )
fname = Request.Form("fname_" & i )
phone = Request.Form("phone_" & i )
lname = Request.Form("lname_" & i )
email = Request.Form("email_" & i )

With Response
.Write address &" | "
.Write zipcode &" | "
.Write city &" | "
.Write fname &" | "
.Write lname
.Write phone &" | "
.Write email
.Write "<br>"
End With

Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.